<?php
class helper_weixin
{
    
    /**
     * 自动处理WX
     * Enter description here ...
     * @return return_type
     * @author Shoulin.wu<460932465@qq.com>
     */
    public static function response($agent_id,$requestXml)
    {
        $return = '<xml>
 <ToUserName><![CDATA[%s]]></ToUserName>
 <FromUserName><![CDATA[%s]]></FromUserName>
 <CreateTime>%d</CreateTime>
 <MsgType><![CDATA[text]]></MsgType>
 <Content><![CDATA[%s]]></Content>
 </xml>';
        $xml = (simplexml_load_string($requestXml,'SimpleXMLElement', LIBXML_NOCDATA));
        $ToUserName = $xml->FromUserName;
        $FromUserName = $xml->ToUserName;
        $time = time();
        $msg = trim(strtolower($xml->Content));
        $weixinid = $xml->FromUserName;
        $result = array();
        if (preg_match('/^bd(\d{11})m(\d.*)$/', $msg,$result))
        {
            $acctname = $result[1];
            $pwd = $result[2];
            $acct = Acctuser::find('acctname = ? and password = ?',$acctname,$pwd)->setColumns('id,weixinid')->getOne();
            if (!$acct->id())
            {
                $context =  '帐号密码错误';
            }else{
                $bind = Weixinbind::find('weixinid = ?',$xml->FromUserName)->getOne();
                if (!$bind->id())
                {
                    $bind = new Weixinbind();
                    $bind->weixinid = $xml->FromUserName;
                }
                    $bind->userid = $acct->id;
                    $bind->bindtime = date('Y-m-d H:i:s');
                    $bind->save();
//                $acct->weixinid = $xml->FromUserName;
//                $acct->wexinbindtime = time(); 
//                $acct->save(0,'update');
                $context =  '绑定成功';
            }
        }else if(preg_match('/^k(\d.*)m(\d.*)s(\d{11})$/', $msg,$result))
        {
            $cardname = $result[1];
            $cardpwd = $result[2];
            $mobile = $result[3];
            $flag = Acctuser::use_card($mobile, $cardname, $cardpwd);
            if ($flag === true)
            {
                $context =  '充值成功';
            }else{
               $context =  $flag;
            }
        }else if(preg_match('/^(\d{11,12})$/', $msg,$result))
        {
            $acct = self::getBindUser($weixinid);
            if (!$acct->id())
            {
                $context =  '请先绑定帐号,回复格式:bd手机号码m密码';
            }else{
                Cbsubmit::call($acct->acctname, $result[1]);
                $context =  '('.$acct->acctname.')正在为您接通中....,请稍后,'.'您当前余额:'.$acct->balance.',有效期:'.$acct->expireddate;
            }
        }else if(preg_match('/^ye$/', $msg,$result))
        {
            $acct = self::getBindUser($weixinid);
            if (!$acct->id())
            {
                $context =  '请先绑定帐号,回复格式:bd手机号码m密码';
            }else{
                $context =  '('.$acct->acctname.')您当前余额:'.$acct->balance.',有效期:'.$acct->expireddate;
            }
        }else{
            
            $context =  '1.充值:k卡号m密码s手机号.2.绑定:bd手机号码m密码.3.拨打电话:13*********,座机请加区号(直接发送被叫)4.查询余额:ye';
        }
        return sprintf($return,$ToUserName,$FromUserName,$time,$context);
    }
    private static function getBindUser($weixinid)
    {
        $bind = Weixinbind::find('weixinid = ?',trim($weixinid))->setColumns('id,userid')->getOne();
        if (!$bind->id())
        {
            return $bind;
        }else{
            $acct = Acctuser::find('id = ?',$bind->userid)->setColumns('id,acctname,balance,expireddate')->getOne();
            return $acct;
        }
    }
}
?>